// 闪电链
// @author:wangff8@qq.com  @date:2024-11-01
library SimulateAttac5 initializer init requires SimulateAttack

    globals

        // 闪电链基础伤害  攻击力*0.5
        // 闪电链  黄色

        // 闪电链等级(默认0)
        integer array ct_sdlLevel
        // 闪电链CD(默认8秒)
        real array ct_sdlCD
        // 闪电链命中最大目标数（默认5）
        integer array ct_sdlMzCount
        // 闪电链伤害增幅
        real array ct_sdlDmgZf

        // 闪电链击中后每0.5秒造成一次攻击力*0.2的闪电伤害
        // 闪电链命中是否持续伤害(默认false)
        boolean array ct_sdlIsChixu
        // 闪电链持续伤害次数（默认3）
        integer array ct_sdlCXCount
        // 闪电链持续伤害增幅
        real array ct_sdlCXDmgZf

        // 闪电链马甲
        unit array majiaSDL

        // 父key uid  子key 1 表示要闪电持续伤害的单位组
        hashtable sdlHash = InitHashtable()
    endglobals

    private function init takes nothing returns nothing
        local integer uid = 0
        loop
            exitwhen uid > PLAYER_NUMBER
            
            set ct_sdlLevel[uid] = 0
            set ct_sdlCD[uid] = 8.0
            set ct_sdlMzCount[uid] = 5
            set ct_sdlDmgZf[uid] = 0.0

            set ct_sdlIsChixu[uid] = false
            set ct_sdlCXCount[uid] = 3
            set ct_sdlCXDmgZf[uid] = 0.0

            set majiaSDL[uid] = CreateUnit(Player(uid), 'N002', GetRectCenterX(getPlayerStartRect(uid)), GetRectCenterY(getPlayerStartRect(uid)), 0.0)

            call SaveGroupHandle(sdlHash, uid, 1, CreateGroup())

            set uid = uid + 1
        endloop
    endfunction

    // 闪电持续伤害单位 玩家1
    private function cxDmgUnit1 takes nothing returns nothing 
        local integer uid = 0
        local unit u = udg_player_select_hero[uid]
        local unit bu = GetEnumUnit()
        local real damage = FF_GetAttack(u) * 0.2 * (ct_sdlCXDmgZf[uid] + 1.0)
        // 灼烧的次数
        local integer count
        
        if bu == null or FF_GetHp(bu) < 1.0 then
            call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
        else
            set count = FF_TEMP_GetSK2(bu) + 1
            call FF_TEMP_SetSK2(bu, count)
            if count <= ct_sdlCXCount[uid] then
                // 单体闪电特效
                // 特效 
                if tx_show_status[uid] == 1 then
                    call DestroyEffect(AddSpecialEffectTarget("tx_shandianbs.mdx", bu, "origin"))
                endif

                call UnitDamageTarget( u, GetEnumUnit(), damage, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_LIGHTNING, WEAPON_TYPE_CLAW_LIGHT_SLICE )
            else
                // 灼烧结束
                call FF_TEMP_SetSK2(bu, 0)
                call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
            endif
        endif
        set u = null
        set bu = null
    endfunction
    // 闪电持续伤害单位 玩家2
    private function cxDmgUnit2 takes nothing returns nothing 
        local integer uid = 1
        local unit u = udg_player_select_hero[uid]
        local unit bu = GetEnumUnit()
        local real damage = FF_GetAttack(u) * 0.2 * (ct_sdlCXDmgZf[uid] + 1.0)
        // 灼烧的次数
        local integer count

        if bu == null or FF_GetHp(bu) < 1.0 then
            call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
        else
            set count = FF_TEMP_GetSK2(bu) + 1
            call FF_TEMP_SetSK2(bu, count)
            if count <= ct_sdlCXCount[uid] then
                // 单体闪电特效
                // 特效 
                if tx_show_status[uid] == 1 then
                    call DestroyEffect(AddSpecialEffectTarget("tx_shandianbs.mdx", bu, "origin"))
                endif

                call UnitDamageTarget( u, GetEnumUnit(), damage, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_LIGHTNING, WEAPON_TYPE_CLAW_LIGHT_SLICE )
            else
                // 灼烧结束
                call FF_TEMP_SetSK2(bu, 0)
                call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
            endif
        endif
        set u = null
        set bu = null
    endfunction
    // 闪电持续伤害单位 玩家3
    private function cxDmgUnit3 takes nothing returns nothing 
        local integer uid = 2
        local unit u = udg_player_select_hero[uid]
        local unit bu = GetEnumUnit()
        local real damage = FF_GetAttack(u) * 0.2 * (ct_sdlCXDmgZf[uid] + 1.0)
        // 灼烧的次数
        local integer count

        if bu == null or FF_GetHp(bu) < 1.0 then
            call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
        else
            set count = FF_TEMP_GetSK2(bu) + 1
            call FF_TEMP_SetSK2(bu, count)
            if count <= ct_sdlCXCount[uid] then
                // 单体闪电特效
                // 特效 
                if tx_show_status[uid] == 1 then
                    call DestroyEffect(AddSpecialEffectTarget("tx_shandianbs.mdx", bu, "origin"))
                endif

                call UnitDamageTarget( u, GetEnumUnit(), damage, false, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_LIGHTNING, WEAPON_TYPE_CLAW_LIGHT_SLICE )
            else
                // 灼烧结束
                call FF_TEMP_SetSK2(bu, 0)
                call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
            endif
        endif
        set u = null
        set bu = null
    endfunction
    // 闪电持续伤害单位 玩家4
    private function cxDmgUnit4 takes nothing returns nothing 
        local integer uid = 3
        local unit u = udg_player_select_hero[uid]
        local unit bu = GetEnumUnit()
        local real damage = FF_GetAttack(u) * 0.2 * (ct_sdlCXDmgZf[uid] + 1.0)
        // 灼烧的次数
        local integer count

        if bu == null or FF_GetHp(bu) < 1.0 then
            call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
        else
            set count = FF_TEMP_GetSK2(bu) + 1
            call FF_TEMP_SetSK2(bu, count)
            if count <= ct_sdlCXCount[uid] then
                // 单体闪电特效
                // 特效 
                if tx_show_status[uid] == 1 then
                    call DestroyEffect(AddSpecialEffectTarget("tx_shandianbs.mdx", bu, "origin"))
                endif

                call UnitDamageTarget( u, GetEnumUnit(), damage, false, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_LIGHTNING, WEAPON_TYPE_CLAW_LIGHT_SLICE )
            else
                // 灼烧结束
                call FF_TEMP_SetSK2(bu, 0)
                call GroupRemoveUnit(LoadGroupHandle(sdlHash, uid, 1), bu)
            endif
        endif
        set u = null
        set bu = null
    endfunction

    // 闪电链 持续伤害 每0.5秒执行一次
    function sdl_Every_05Second takes nothing returns nothing
        local integer uid = 0
        if ct_sdlIsChixu[uid] then
            call ForGroup(LoadGroupHandle(sdlHash, 0, 1), function cxDmgUnit1)
        endif
        set uid = 1
        if ct_sdlIsChixu[uid] then
            call ForGroup(LoadGroupHandle(sdlHash, 1, 1), function cxDmgUnit2)
        endif
        set uid = 2
        if ct_sdlIsChixu[uid] then
            call ForGroup(LoadGroupHandle(sdlHash, 2, 1), function cxDmgUnit3)
        endif
        set uid = 3
        if ct_sdlIsChixu[uid] then
            call ForGroup(LoadGroupHandle(sdlHash, 3, 1), function cxDmgUnit4)
        endif
    endfunction

    // 释放闪电链
    function releseSDL takes integer uid returns nothing
        local unit bu = getNearestUnit(uid)

        if getDistanceUnit(udg_player_select_hero[uid], bu) < 3000 then
            // call BJDebugMsg("释放闪电链")
            call IssueTargetOrder(majiaSDL[uid], "chainlightning", bu)
        endif
        
        set bu = null
    endfunction
    
endlibrary



